package com.woniu.exceptions;

import com.woniu.entity.ResponseEntity;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/06 16:21
 */
@RestControllerAdvice
public class GlobalExceptionControllerAdvice {

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(ShiroException.class)
    public ResponseEntity handler401(ShiroException e){
        return new ResponseEntity(HttpStatus.UNAUTHORIZED.value(),e.getMessage());
    }

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthenticatedException.class)
    public ResponseEntity handler(UnauthenticatedException e){
        return new ResponseEntity(HttpStatus.UNAUTHORIZED.value(),e.getMessage());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity globalException(Throwable e){
        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(),e.getMessage());
    }

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity authException(AuthenticationException e){
        return new ResponseEntity(HttpStatus.UNAUTHORIZED.value(),e.getMessage());
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(LoginException.class)
    public ResponseEntity loginException(Throwable e){
        return new ResponseEntity(HttpStatus.FORBIDDEN.value(),e.getMessage());
    }

}
